import logging
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from application.config.fastapi import FastapiConfig, base_router
from .log_config import setup_logging, log_requests
from .router_v1 import v1_router

app = FastAPI(**FastapiConfig.__dict__)

# 日志配置
info_logger, error_logger = setup_logging()
middleware = log_requests(info_logger, error_logger)
app.middleware("http")(middleware)
info_logger.info("test")  # 手动写入日志，先引入setup_logging()
error_logger.error("test")  # 手动写入日志，先引入setup_logging()

# 跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

# 根路由
app.include_router(base_router, tags=["根路径"])
app.include_router(v1_router, prefix='/api')

# 静态文件路径
app.mount('/api/front', StaticFiles(directory='static/front'), name='front')  # 前端文件
